package com.tsd.base.service.impl;

import com.tsd.base.dao.BaseCodeRuleItemMapper;
import com.tsd.base.entity.BaseCodeRuleItem;
import com.tsd.base.entity.BaseCodeRuleItemExt;
import com.tsd.base.service.BaseCodeRuleItemService;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.vo.PageBean;
import com.tsd.service.BaseServiceImpl;
import com.tsd.system.entity.SysUser;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 描述：BaseCodeRuleItem 服务实现层
 *
 * @author zsy
 * @date 2020/12/29 15:22:59
 */
@Service
public class BaseCodeRuleItemServiceImpl extends BaseServiceImpl implements BaseCodeRuleItemService {

    @Resource
    private BaseCodeRuleItemMapper baseCodeRuleItemMapper;

    @Override
    public BaseCodeRuleItem getBaseCodeRuleItemById(Long id) throws Exception {
        return baseCodeRuleItemMapper.selectByPrimaryKey(id);
    }

    @Override
    public BaseCodeRuleItemExt queryBySid(String sid) throws Exception {
        return baseCodeRuleItemMapper.selectBySid(sid);
    }

    @Override
    public void deleteBaseCodeRuleItem(Long id, SysUser opUser) throws Exception {
        BaseCodeRuleItem old = baseCodeRuleItemMapper.selectByPrimaryKey(id);
        super.checkEmpty(old, "该记录不存在");
        super.autoInjectBaseData(old, opUser, this.TYPE_DELETE);
        baseCodeRuleItemMapper.updateByPrimaryKeySelective(old);
    }

    @Override
    public void saveBaseCodeRuleItem(BaseCodeRuleItem record, SysUser opUser) throws Exception {
        if (HlpUtils.isEmpty(record.getId())) {
            super.checkEmpty(record.getCode_rule_sid(), "编码规则主表ID不能为空");
            super.checkEmpty(record.getType(), "类型不能为空");
            super.autoInjectBaseData(record, opUser, this.TYPE_CREATE);
            baseCodeRuleItemMapper.insertSelective(record);
        } else {
            super.autoInjectBaseData(record, opUser, this.TYPE_UPDATE);
            baseCodeRuleItemMapper.updateByPrimaryKeySelective(record);
        }
    }

    @Override
    public List<BaseCodeRuleItemExt> findBaseCodeRuleItems(Map<String, Object> params, PageBean page) throws Exception {
        return baseCodeRuleItemMapper.selectByPage(params, page);
    }

    @Override
    public List<BaseCodeRuleItemExt> findBaseCodeRuleItems(Map<String, Object> params) throws Exception {
        return baseCodeRuleItemMapper.selectByParams(params);
    }


    @Override
    public void batchDeleteBaseCodeRuleItem(List<Long> idList, SysUser opUser) throws Exception {
        for (Long id : idList) {
            deleteBaseCodeRuleItem(id, opUser);
        }
    }
}
